﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sissonix.ExtRest.Exception;
using System.Web;

namespace Sissonix.ExtRest.Processors
{
    /// <summary>
    /// This is a base class that provides common functionality for 
    /// all classes that will handle incoming URI's.
    /// </summary>
    class BaseRestProcessor : IHttpHandler
    {

        /// <summary>
        /// The MasterRestProcessor and its inheriting classes
        /// are not reusable because there are class level variables
        /// that will be specific to each request that is recieved.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// This is the main method that will be called by the
        /// RequestProcessor.  It must be defined by the class that
        /// inherits from MasterRestProcessor.
        /// </summary>
        /// <param name="context">The context.</param>
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpMethods.Method currentMethod = HttpMethods.ParseHttpMethodType(req.RequestType);
            try
            {
                switch (currentMethod)
                {
                    case HttpMethods.Method.DELETE:
                        ProcessDelete(context); break;
                    case HttpMethods.Method.GET:
                        ProcessGet(context); break;
                    case HttpMethods.Method.POST:
                        ProcessPost(context); break;
                    case HttpMethods.Method.PUT:
                        ProcessPut(context); break;
                    case HttpMethods.Method.HEAD:
                    case HttpMethods.Method.OPTIONS:
                    case HttpMethods.Method.NotHandled:
                    default:
                        break;
                }
            }
            catch (RestException e)
            {
            }
        }

        public virtual void ProcessDelete(HttpContext context) { }
        public virtual void ProcessGet(HttpContext context) { }
        public virtual void ProcessPost(HttpContext context) { }
        public virtual void ProcessPut(HttpContext context) { }
        public virtual void ProcessOther(HttpContext context) { }
    }
}
